package com.ccf.business.etl.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.common.utils.constant.Constants;
import com.common.model.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.common.model.pojo.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import com.ccf.business.etl.model.RuleSynonym;
import com.ccf.business.etl.service.IRuleSynonymService;
import com.common.model.result.PageResult;
import com.common.model.result.Result;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 *
 * @author zhaoj
 * @date 2021-11-29 10:41:10
 */
@Slf4j
@RestController
@RequestMapping("/rulesynonym")
@Api(tags = "同义词管理")
public class RuleSynonymController {
    @Autowired
    private IRuleSynonymService ruleSynonymService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询列表")
    @GetMapping
    public PageResult list(Query query) {
        return ruleSynonymService.findList(query);
    }


    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id) {
        RuleSynonym model = ruleSynonymService.getById(id);
        return Result.succeed(model.toVo(), "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping
    public Result save(@RequestBody RuleSynonym ruleSynonym) {
        //校验主词
        if (ruleSynonymService.isExistField(ruleSynonym.getId(), ruleSynonym.getSubject(),
                "subject")){
            return Result.failed("主词已存在");
        }
        //校验同义词是否存在
        String[] synonyms =  StrUtil.split(ruleSynonym.getSynonyms(), Constants.COMMA);
        int size = Arrays.stream(synonyms)
                .collect(Collectors.groupingBy(s -> s, Collectors.summingInt(s -> 1)))
                .entrySet()
                .stream()
                .filter(entry -> entry.getValue() > 1)
                .collect(Collectors.toList())
                .size();
        if(size > 0) {
            return Result.failed("存在相同的同义词");
        }

        ruleSynonym.setCreator(UserInfo.DEFAULT_USER_ID);
        ruleSynonymService.saveOrUpdate(ruleSynonym);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        if(StrUtil.isEmpty(id)) {
            return Result.failed("请选择数据");
        }
        return Result.status(ruleSynonymService.removeByIds(Arrays.asList(StrUtil.split(id, Constants.COMMA))),"","删除成功", "删除失败");
    }

    /**
     * 删除校验
     */
    @ApiOperation(value = "删除校验")
    @GetMapping("/{id}/validate")
    public Result deleteValidate(@PathVariable String id) {
        if(StrUtil.isEmpty(id)) {
            return Result.failed("请选择数据");
        }
        return Result.succeed(ruleSynonymService.deleteValidate(id));
    }


    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/import")
    public Result importSynonym(MultipartFile file) throws IOException {
        return Result.status(ruleSynonymService.importSynonym(file), "", "导入成功", "导入失败");
    }
}
